package com.yx.yinshanjinniu.fragments;

import android.view.View;
import android.widget.ListView;
import android.widget.TextView;

import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.constant.SpinnerStyle;
import com.scwang.smartrefresh.layout.footer.ClassicsFooter;
import com.scwang.smartrefresh.layout.listener.OnLoadMoreListener;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;
import com.yx.yinshanjinniu.R;
import com.yx.yinshanjinniu.adapters.FollowListAdapter;
import com.yx.yinshanjinniu.base.BaseFragment;
import com.yx.yinshanjinniu.bean.FollowListBean;
import com.yx.yinshanjinniu.net.ActionCallbackListener;
import com.yx.yinshanjinniu.utils.ToastUtil;

import java.util.List;

/**
 * Created by Mr on 2018/11/8.
 */

public class MyFollowFragment extends BaseFragment implements FollowListAdapter.FollowListListener, OnRefreshListener, OnLoadMoreListener {
    private SmartRefreshLayout refresh_layout;
    private TextView tv_empty;
    private ListView lv_list;
    private FollowListAdapter mAdapter;
    private int mPageIndex = 1;

    @Override
    protected void initVariable() {
        refresh_layout.setRefreshFooter(new ClassicsFooter(mContext).setSpinnerStyle(SpinnerStyle.Translate));
        mAdapter = new FollowListAdapter(mContext, true, this);
        lv_list.setAdapter(mAdapter);
    }

    @Override
    protected int setContentView() {
        return R.layout.fragment_my_follow;
    }

    @Override
    protected void findViews(View rootView) {
        refresh_layout = rootView.findViewById(R.id.refresh_layout);
        lv_list = rootView.findViewById(R.id.lv_list);
        tv_empty = rootView.findViewById(R.id.tv_empty);
    }

    @Override
    protected void setListeners() {
        refresh_layout.setOnRefreshListener(this);
        refresh_layout.setOnLoadMoreListener(this);
    }

    @Override
    protected void refreshDataSource() {
        getMyFollowList(1);
    }

    @Override
    public void onLoading() {
        baseActivity.loading();
    }

    @Override
    public void onCancelLoading() {
        baseActivity.cancelLoading();
    }

    @Override
    public void onLogout() {
        onLogoutStatus();
    }

    @Override
    public void onFollowChanged(boolean isAdd, FollowListBean bean) {
        mAdapter.removeItem(bean);
        if (mAdapter.isEmpty()) {
            tv_empty.setVisibility(View.VISIBLE);
        }else{
            tv_empty.setVisibility(View.GONE);
        }
    }

    @Override
    public void onRefresh(RefreshLayout refreshlayout) {
        getMyFollowList(1);
    }

    @Override
    public void onLoadMore(RefreshLayout refreshlayout) {
        getMyFollowList(mPageIndex + 1);
    }

    private void getMyFollowList(final int page) {
        mAppAction.followList(1, page, new ActionCallbackListener<List<FollowListBean>>() {
            @Override
            public void onSuccess(List<FollowListBean> data) {
                refresh_layout.finishRefresh(1000);
                refresh_layout.finishLoadMore(1000);

                if (page == 1) {
                    mAdapter.setDataSource(data);
                    mPageIndex = page;
                } else if (page > mPageIndex) {
                    mAdapter.addList(data);
                    mPageIndex = page;
                }
                if (mAdapter.getCount() == 0) {
                    tv_empty.setVisibility(View.VISIBLE);
                } else {
                    tv_empty.setVisibility(View.GONE);
                }
                if (mAdapter.getCount() == mPageIndex * 10) {
                    refresh_layout.setEnableLoadMore(true);
                } else {
                    refresh_layout.setEnableLoadMore(false);
                }
            }

            @Override
            public void onFailure(String errorCode, String message) {
                refresh_layout.finishRefresh();
                refresh_layout.finishLoadMore();
                ToastUtil.showToast(mContext, message);
            }
        });
    }
}
